#!/bin/bash

# Runs a textfile collector.

script_dir=$(dirname $0)
source ${script_dir}/ENV
file_path=$1
metric=`basename "$1"`
shift
script="${script_dir}/${file_path}"
prom_file="/opt/fonsview/3RD/node_exporter/textfile_collector/$metric".prom

if [[ ! -x "$script" || -d "$script" ]]; then
#   echo "ERROR: Can't find script for '$metric'. Aborting."
  exit 1
fi

VALUE=`"$script" "$@"`
if [[ ! -n $VALUE ]]; then
    exit 1
else
    echo "# TYPE ${metric} gauge"> "$prom_file".$$
    echo "${metric} ${VALUE}" >> "$prom_file".$$ && mv "$prom_file".$$ "$prom_file"
fi 
